package main.algorithm;

import org.junit.Test;

/**
 * 数据算法
 * 
 * @author chenggm
 * @create 2017-02-09 9:50
 **/
public class DataAlgorithm {

	@Test
	public void test() throws Exception {
		double result = sqrt(9);
		System.out.println(result);
	}

	/**
	 *判断一个数是否为素数
	 *
	 * @param n the n
	 * @return the boolean
	 */
	public static boolean isPrime(Integer n) {
		if (n < 2) {
			return false;
		}
		for (int i = 0; i * i < n; i++) {
			if (n % i == 0) {
				return false;
			}
		}
		return true;
	}
	public static double sqrt(double c){
		if(c<0){
			return Double.NaN;
		}
		double err = 1e-15;
		double t = c;
		while (Math.abs(t-c/t)>err*t){
			t=(c/t+t)/2.0;
		}
		return t;
	}
}
